<?php 
namespace app\user\taglib;

use think\template\TagLib;

class user extends Taglib
{

    // 标签定义
    protected $tags = [
        // 标签定义： attr 属性列表 close 是否闭合（0 或者1 默认1） alias 标签别名 level 嵌套层次
        // 列表
        'list'              =>  ['attr'=>'order,limit,return','close'=>0],
        'my'                =>  ['attr'=>'return','close'=>0],
        'user'              =>  ['attr'=>'id,return','close'=>0],
    ];

    // 用户列表
    public function taglist($tag,$content) {
        if (empty($tag['return'])) {//return为空 返回空
            $tag['return'] = '_data';
        }
        $str = '';
        $str .="<?php ";
        $str .="\${$tag['return']} = [];";
        $str .="\$_where = array();";
        $str .="\$_m = new \\app\\user\\model\\User();";
        if (isset($tag['order'])) {
            $str .="\$_m -> order('{$tag['order']}');";
        }else{
            $str .="\$_m -> order('id desc');";
        }
        $str .="\$_where['status'] = array('eq',1);";
        $str .="\$_m -> where(\$_where);";
        // 解析limit限制属性
        if (isset($tag['limit'])) {
            if ('$' == substr($tag['limit'],0,1)) {
                $tag['limit']   = $this->autoBuildVar($tag['limit']);
                $str .="\$_m -> limit({$tag['limit']});";
            }else{
                $str .="\$_m -> limit('{$tag['limit']}');";
            }
        }
        $str .="\${$tag['return']} = \$_m -> field(true) -> select();";
        $str .=" ?>";
        return $str;
    }

    // 自己
    public function tagmy($tag,$content) {
        if (empty($tag['return'])) {//return为空 返回空
            $tag['return'] = '_data';
        }
        $str = '';
        $str .="<?php ";
        $str .="\${$tag['return']} = [];";
        $str .="\${$tag['return']} = \\app\\user\\model\\User::get(session('user_id'));";
        $str .=" ?>";
        return $str;
    }

    // 任意用户
    public function taguser($tag,$content) {
        if (empty($tag['id'])) {
            return '';
        }
        if (empty($tag['return'])) {
            $tag['return'] = '_data';
        }
        $str = '';
        $str .="<?php ";
        $str .="\${$tag['return']} = [];";
        if (isset($tag['id'])) {
            if ('$' == substr($tag['id'],0,1)) {
                $tag['id']   = $this->autoBuildVar($tag['id']);
                $str .="\${$tag['return']} = \\app\\user\\model\\User::get({$tag['id']});";
            }else{
                $str .="\${$tag['return']} = \\app\\user\\model\\User::get('{$tag['id']}');";
            }
        }
        $str .=" ?>";
        return $str;
    }

}